layui.use(['element', 'table', 'laytpl'], function () {
    var $ = layui.$,
        layer = layui.layer;

    $(function () {
        $( "#menu" ).menu();
    });


    var zTreeObj;
    // zTree 的参数配置
    var setting = {
        view: {
            showIcon: false,//设置是否显示节点图标
            showLine: false,//不显示连接线，默认值true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onRightClick: onRightClick
        }
    };

    function onRightClick(event, treeId, treeNode) {
        var $delete = $(".delete");
        if (treeNode==null || treeNode==undefined||treeNode==""){
            return;
        }
        var id = treeNode.id;
        if (id ==0){
          $("#li-delete").hide()
        }else {
            $("#li-delete").show()
        }
        var $menu = $("#menu");
        //获取当前子节点的唯一标识id
        // 获取窗口尺寸
        var winWidth = $(document).width();
        var winHeight = $(document).height();
        // 鼠标点击位置坐标
        var mouseX = event.pageX;
        var mouseY = event.pageY;
        // ul标签的宽高
        var menuWidth = $menu.width();
        var menuHeight =$menu.height();
        // 最小边缘margin(具体窗口边缘最小的距离)
        var minEdgeMargin = 10;
        // 以下判断用于检测ul标签出现的地方是否超出窗口范围
        // 第一种情况：右下角超出窗口
       var menuLeft;
       var menuTop;
        if(mouseX + menuWidth + minEdgeMargin >= winWidth && mouseY + menuHeight + minEdgeMargin >= winHeight) {
            menuLeft = mouseX - menuWidth - minEdgeMargin + "px";
            menuTop = mouseY - menuHeight - minEdgeMargin + "px";
        } else if(mouseX + menuWidth + minEdgeMargin >= winWidth) {
            // 第二种情况：右边超出窗口
            menuLeft = mouseX - menuWidth - minEdgeMargin + "px";
            menuTop = mouseY + minEdgeMargin + "px";
        } else if(mouseY + menuHeight + minEdgeMargin >= winHeight) {
            // 第三种情况：下边超出窗口
            menuLeft = mouseX + minEdgeMargin + "px";
            menuTop = mouseY - menuHeight - minEdgeMargin + "px";
        } else {
            // 其他情况：未超出窗口
            menuLeft = mouseX + minEdgeMargin -6+ "px";
            menuTop = mouseY + minEdgeMargin-6 + "px";
        };
        // ul菜单出现
        $menu.css({
            "left": menuLeft,
            "top": menuTop
        }).show();
        var $save = $(".save");
        $save.data("id",id);
        $delete.data("id",id);
        //是否还有子节点
        $delete.data("zi",treeNode.children ? true : false);
    }
    $("#menu").mouseleave(function(){
        $("#menu").hide()
    });

    $(".save").on('click',function () {
        $("#menu").hide();
        var id = $(this).data("id");
        oo = layer.open({
            type: 2,
            content: '/department/add?id=' + id,
            area: ['500px', '225px'],
            title: '部门设置'
        });
    });

    $(".delete").on('click',function () {
        $("#menu").hide()
        var $delete = $(this);
        var id = $delete.data("id");
        var zi = $delete.data("zi");
        if(zi || "0"==id){
            var message1;
            if (zi) {
                message1 = "请您先删除该部门下的子部门";
            }
            if ("0"==id){
                message1 = "该节点不能被删除";
            }
            layer.open({
                type: 0,
                content: message1,
                area: ['400px', '180px'],
                title: '提示'
            });
            return;
        }
        //查询该部门下是否有从业人员
        var start=true;
        $.ajax({
            url: "/department/containEmployee?id=" + id,
            type: 'get',
            dataType: 'json',
            success: function (data) {
                if (data.code == 200) {
                    layer.open({
                        type: 0,
                        content: data.message,
                        area: ['400px', '180px'],
                        title: '提示'
                    });
                }
                if (data.code == 0){
                    var message2 = "您确定要删除该部门吗？";
                    cc=layer.open({
                        type: 1,
                        content: message2,
                        area: ['400px', '180px'],
                        title: '提示',
                        btn: ['确定'], //可以无限个按钮
                        yes: function (index, layero) {
                            $.ajax({
                                url: "/department/delete?id=" + id,
                                type: 'get',
                                dataType: 'json',
                                success: function (data) {
                                    if (data.code == 200) {
                                        layer.msg(data.message);
                                        layer.close(cc);
                                        location.reload();
                                    }
                                    if (data.data.code == 0) {
                                        layer.msg(data.message)
                                    }
                                },
                                error: function (e) {
                                    console.log("删除部门失败");
                                }
                            });
                        }
                    });
                }
            },
            error: function (e) {
                console.log("删除部门失败");
            }
        });
    });

    $.ajax({
        url: "/department/tree",
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data.code == 200) {
                $.fn.zTree.init($("#DepartmentTree"), setting, data.data);
            }
            if (data.data.code == 0) {
                layer.msg(data.message)
            }
        },
        error: function (e) {
            console.log("部门树加载失败");
        }
    });

});